package com.ayi.constants;

import com.ayi.bean.Book;
import com.ayi.bean.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ayingzz
 * @create 2021/11/28 14:23
 */
public class BookShopConstants {

    public static class ConstantValue {

        static final String baseAddress = "http://localhost:8080/JavaBookShop/";

    }

    public static class ConstantMethods {

        /**
         * 得到初始化状态Map，以供 修改/保存 功能使用
         *
         * @param map
         * @param conn
         * @param sql
         * @return
         */
        public static Map<Integer, Integer> getInitMap(Map<Integer, Integer> map, Connection conn, String sql) {
            try {
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
                int n = 0;
                while (rs.next()) {
                    map.put(rs.getInt("id"), -1);
                    n++;
                }
                return map;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return null;
        }

        /**
         * 得到一本书
         *
         * @param rs
         * @return
         * @throws SQLException
         */
        public static Book getBook(ResultSet rs) throws SQLException {
            Book book = new Book();
            if (rs.next()) {
                setBook(rs, book);
            }
            return book;
        }

        /**
         * 得到一本书
         *
         * @param rs
         * @return
         * @throws SQLException
         */
        public static Book getBookWithOutNext(ResultSet rs) throws SQLException {
            Book book = new Book();
            setBook(rs, book);
            return book;
        }

        /**
         * 在Books表里得到一些书
         *
         * @param rs
         * @return
         * @throws SQLException
         */
        public static List<Book> getBooks(ResultSet rs) throws SQLException {
            List<Book> resList = new ArrayList<>();
            while (rs.next()) {
                Book book = new Book();
                setBook(rs, book);
                resList.add(book);
            }
            return resList;
        }


        /**
         * 得到一个用户下订单的书（含具体数量）
         *
         * @param rs
         * @param username
         * @return
         * @throws SQLException
         */
        public static List<Book> getBooksWithSettingNum(ResultSet rs, String username) throws SQLException {
            List<Book> resList = new ArrayList<>();
            while (rs.next()) {
                Book book = new Book(username, rs.getInt("id"));
                setBook(rs, book);
                resList.add(book);
            }
            return resList;
        }

        /**
         * @param rs
         * @param book
         * @throws SQLException
         */
        private static void setBook(ResultSet rs, Book book) throws SQLException {
            book.setId(rs.getInt("id"));
            book.setName(rs.getString("name"));
            book.setAuthor(rs.getString("author"));
            book.setType(rs.getString("type"));
            book.setDetails(rs.getString("details"));
            book.setPrice(rs.getDouble("price"));
            book.setInventory(rs.getInt("inventory"));
            book.setImgPath(rs.getString("imgpath"));
        }

        /**
         * 得到一个用户
         *
         * @param rs
         * @return
         * @throws SQLException
         */
        public static User getUser(ResultSet rs) throws SQLException {
            User user = new User();
            if (rs.next()) {
                setUser(rs, user);
            }
            return user;
        }

        /**
         * 得到一些用户
         *
         * @param rs
         * @return
         * @throws SQLException
         */
        public static List<User> getUsers(ResultSet rs) throws SQLException {
            List<User> list = new ArrayList<>();
            while (rs.next()) {
                User user = new User();
                setUser(rs, user);
                list.add(user);
            }
            return list;
        }

        /**
         * @param rs
         * @param user
         * @throws SQLException
         */
        private static void setUser(ResultSet rs, User user) throws SQLException {
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setAdmin(rs.getBoolean("isAdmin"));
            user.setStop(rs.getBoolean("isStop"));
        }

        /**
         *
         * @param username
         * @param conn
         * @param sb
         * @param sql
         * @return
         */
        public static boolean executeSqlUpdateCartList(String username, Connection conn, StringBuilder sb, String sql) {
            try {
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, sb.toString());
                ps.setString(2, username);
                ps.execute();
                return true;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }

        /**
         * 截取目标图书id在购物车里对应的数量，返回起始位置
         * @param sb
         * @param id
         * @return
         */
        public static int[] getEndAndStartIndexInCartList(StringBuilder sb, int id) {
            int end = -1, start = -1;
            String idStr = id + "";
            for (int i = 0, n = sb.length(); i < n; i++) {
                if (end == -1 && sb.charAt(i) == (idStr.charAt(0))) {
                    int k = 1;
                    int m = idStr.length();
                    int j = i + 1;
                    for (; k < m; j++, k++) {
                        if (sb.charAt(j) == (idStr.charAt(k))) {
                            continue;
                        } else {
                            break;
                        }
                    }
                    if (k == m && sb.charAt(j) == '=') {
                        end = j + 1;
                    }
                }
                if (end != -1){
                    if (sb.charAt(i) == '#') {
                        start = i;
                        break;
                    }
                }
            }
            return new int[]{end, start};
        }

    }

}
